package com.bedpotato.http;

import com.bedpotato.po.ProxyPO;
import com.gargoylesoftware.htmlunit.AjaxController;
import com.gargoylesoftware.htmlunit.BrowserVersion;
import com.gargoylesoftware.htmlunit.CookieManager;
import com.gargoylesoftware.htmlunit.WebClient;
import com.gargoylesoftware.htmlunit.html.HtmlPage;

public class HtmlUnitMethod {
	public String get(String url) throws Exception{
		AjaxController ajaxController = new AjaxController();
		CookieManager cookieManager = new CookieManager();
		cookieManager.setCookiesEnabled(true);
		WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6);
		webClient.setAppletEnabled(true);
		webClient.setCssEnabled(false);
		webClient.setJavaScriptEnabled(true);
		webClient.setAjaxController(ajaxController);
		webClient.setThrowExceptionOnScriptError(false);
		webClient.setThrowExceptionOnFailingStatusCode(false);
		webClient.setCookieManager(cookieManager);
		webClient.setUseInsecureSSL(true);
		HtmlPage page = webClient.getPage(url);
		return page.getPage().asText();
	}
	public String getByAnonymousProxy(String url, ProxyPO proxyPO)
			throws Exception {
		AjaxController ajaxController = new AjaxController();
		CookieManager cookieManager = new CookieManager();
		cookieManager.setCookiesEnabled(true);
		WebClient webClient = new WebClient(BrowserVersion.FIREFOX_3_6, proxyPO.getIp(), Integer.valueOf(proxyPO.getPort()));
		webClient.setAppletEnabled(true);
		webClient.setTimeout(12000);
		webClient.setCssEnabled(false);
		webClient.setJavaScriptEnabled(true);
		webClient.setAjaxController(ajaxController);
		webClient.setThrowExceptionOnScriptError(false);
		webClient.setThrowExceptionOnFailingStatusCode(false);
		webClient.setCookieManager(cookieManager);
		webClient.setUseInsecureSSL(true);
		HtmlPage page = webClient.getPage(url);
		return page.getPage().asText();
	}
}
